import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HomeController extends GetxController {
  HomeController();

  TextEditingController oneTextController = TextEditingController();
  TextEditingController twoTextController = TextEditingController();
  TextEditingController threeTextController = TextEditingController();
  TextEditingController resultTextController = TextEditingController();

  final FocusNode oneFocusNode = FocusNode();
  final FocusNode twoFocusNode = FocusNode();
  final FocusNode threeFocusNode = FocusNode();
  final FocusNode resultFocusNode = FocusNode();

  bool isShowResult = false;

  List<List<dynamic>> correctDtaList = [];
  List<List<dynamic>> errorDtaList = [];

  void identify() {
    int one = int.parse(oneTextController.text);
    int two = int.parse(twoTextController.text);
    int three = int.parse(threeTextController.text);
    int result = int.parse(resultTextController.text);

    Set allSet = {};
    Set correctSet = {};
    // 任意三个数相加 = 10
    // 任意五个数相加 和范围是 10 ～ 28，比如 尾数 6，可能是 16 或者 26
    for (int four = 0; four < 10; four++) {
      for (int five = 0; five < 10; five++) {
        // 筛选范围和在 10 ～ 28 的组合, [one, two, three, four, five]
        int sum = one + two + three + four + five;
        List<int> numberList = [four, five];
        numberList.sort((a, b) => a.compareTo(b));
        String numberStr = numberList.toString();
        allSet.add(numberStr);

        // 10 + result == sum || 2 * 10 + result == sum
        if ((sum - result) % 10 == 0) {

          // 筛选任意三数和等于 10 的组合
          if (one + two + three == 10) {
            correctSet.add(numberStr);
          }

          if (one + two + four == 10) {
            correctSet.add(numberStr);
          }

          if (one + two + five == 10) {
            correctSet.add(numberStr);
          }

          if (one + three + four == 10) {
            correctSet.add(numberStr);
          }

          if (one + three + five == 10) {
            correctSet.add(numberStr);
          }

          if (two + three + four == 10) {
            correctSet.add(numberStr);
          }

          if (two + three + five == 10) {
            correctSet.add(numberStr);
          }

          if (three + four + five == 10) {
            correctSet.add(numberStr);
          }
        }
      }
    }
    for (String item in correctSet) {
      correctDtaList.add(json.decode(item));
    }
    for(String item in allSet){
      if(!correctSet.contains(item)){
        errorDtaList.add(json.decode(item));
      }
    }
    update(["home-page"]);
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    oneTextController.dispose();
    twoTextController.dispose();
    threeTextController.dispose();
    resultTextController.dispose();
    oneFocusNode.dispose();
    twoFocusNode.dispose();
    threeFocusNode.dispose();
    resultFocusNode.dispose();
    super.onClose();
  }
}
